﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Microsoft.SqlServer.Server;
using TSComm.Utilities.Datas;

namespace JSTT_DATA_ENTITY.XMLDatas
{

    /// <summary>
    /// Represent DataTypeRule.DF_RULEDescription Element
    /// <DF_RULEDescription>
    ///     <RuleName>RULENAME</RuleName>
    ///     <RuleDataSendMode>SocketData</RuleDataSendMode>
    ///     <RuleDataMinLength>1024</RuleDataMinLength>
    ///     <RuleDataMaxLength>1024</RuleDataMaxLength>
    /// </DF_RULEDescription> 
    /// </summary>
    public class JSTT_DataRuleDescription
    {
        private string _ruleName;
        private string _ruleDataSendMode;
        private int _ruleDataMinLength;
        private int _ruleDataMaxLength;

        public JSTT_DataRuleDescription()
        {
        }

        public JSTT_DataRuleDescription(string ruleName, string ruleDataSendMode, int ruleDataMinLength,
            int ruleDataMaxLength)
        {
            this._ruleName = ruleName;
            this._ruleDataSendMode = ruleDataSendMode;
            this._ruleDataMinLength = ruleDataMinLength;
            this._ruleDataMaxLength = ruleDataMaxLength;
        }

        /// <summary>
        /// DataTypeRule.DF_RULEDescription.RuleName Element
        /// </summary>
        [XmlElement("RuleName")]
        public string RuleName
        {
            get { return _ruleName; }
            set { _ruleName = value; }
        }

        /// <summary>
        /// DataTypeRule.DF_RULEDescription.RuleDataSendMode Element
        /// </summary>
        [XmlElement("RuleDataSendMode")]
        public string RuleDataSendMode
        {
            get { return _ruleDataSendMode; }
            set { _ruleDataSendMode = value; }
        }

        /// <summary>
        /// DataTypeRule.DF_RULEDescription.RuleDataMinLength Element 
        /// </summary>
        [XmlElement("RuleDataMinLength")]
        public int RuleDataMinLength
        {
            get { return _ruleDataMinLength; }
            set { _ruleDataMinLength = value; }
        }

        /// <summary>
        /// DataTypeRule.DF_RULEDescription.RuleDataMinLength Element 
        /// </summary>
        [XmlElement("RuleDataMaxLength")]
        public int RuleDataMaxLength
        {
            get { return _ruleDataMaxLength; }
            set { _ruleDataMaxLength = value; }
        }
    }


    /// <summary>
    /// Represent DataTypeRule.DF_TableInfo Element 
    /// <DF_TableInfo>
    ///     <TableName>TF_JSTT_DATAS_T01</TableName>
    ///     <TableColumns>
    ///         <Column Name="ID" DisplayName="" ColumnType="int" AutoIncrease=1 />
    ///     </TableColumns>
    /// </DF_TableInfo>
    /// </summary>
    public class JSTT_DataRuleTableInfo
    {
        private string _historyTableName;
        private string _currencyTableName;
        //private DataRuleTableColumns _elemenTableColumns;
        private List<DataRuleTableColumn> dtColumns;

        public JSTT_DataRuleTableInfo()
        {
            _historyTableName = _currencyTableName = "";
            dtColumns = new List<DataRuleTableColumn>();
        }

        public JSTT_DataRuleTableInfo(string historyTableName, string currencyTableName)
        {
            _currencyTableName = currencyTableName;
            _historyTableName = historyTableName;

            dtColumns = new List<DataRuleTableColumn>();
        }

        public void AddTableColumn(DataRuleTableColumn dtc)
        {
           dtColumns.Add(dtc); 
        }

        public void AddTableColumn(string name, string displayName, string columnType,
            int typeLength, int autoIncrease = 0, string columnIsKey = "false")
        {
            dtColumns.Add(new DataRuleTableColumn(name, displayName, columnType, typeLength,
                autoIncrease, columnIsKey));
        }

        /// <summary>
        /// DataTypeRule.DF_TableInfo.HistoryTableName Element
        /// </summary>
        [XmlElement("HistoryTableName")]
        public string HistoryTableName
        {
            get { return _historyTableName; }
            set { _historyTableName = value; }
        }

        /// <summary>
        /// DataTypeRule.DF_TableInfo.CurrencyTableName Element
        /// </summary>
        [XmlElement("CurrencyTableName")]
        public string CurrencyTableName
        {
            get { return _currencyTableName; }
            set { _currencyTableName = value; }
        }
        
        /// <summary>
        /// DataTypeRule.DF_TableInfo.TableColumns Element 
        /// </summary>
        [XmlArrayItem("Column", typeof(DataRuleTableColumn))]
        [XmlArray("TableColumns")]
        public DataRuleTableColumn[] TableColumns
        {
            get
            {
                return dtColumns.ToArray();
            }
            set
            {
                foreach (DataRuleTableColumn dataRuleTableColumn in value)
                {
                    dtColumns.Add(dataRuleTableColumn);
                }
            }
        }
    }


    
   

    /// <summary>
    /// DF_RULE_DATAS所存储的XML格式
    /// <DataTypeRule>
    ///     <DF_RULEDescription>
    ///         ...
    ///     </DF_RULEDescription>
    ///     <DF_TableInfo>
    ///         ...
    ///     </DF_TableInfo>
    ///     <DF_DataRules>
    ///         ...
    ///     </DF_DataRules>
    /// </DataTypeRule>
    /// </summary>
    [XmlRoot("DataTypeRule")]
    public class JSTT_DataRuleFormats 
    {
        private JSTT_DataRuleDescription _jstt_dataRuleDescription;
        private JSTT_DataRuleTableInfo _jstt_dataRuleTableInfo;
        private JSTT_DataRuleItems _jstt_dataRuleItems;
        private JSTT_EXPTDataItems _jstt_exptDataItems;

        [XmlElement("JSTT_RuleDescription")]
        public JSTT_DataRuleDescription JSTT_RuleDescription
        {
            get { return _jstt_dataRuleDescription; }
            set { _jstt_dataRuleDescription = value; }
        }

        [XmlElement("JSTT_RuleTableIinfo")]
        public JSTT_DataRuleTableInfo JSTT_RuleTableInfo
        {
            get { return _jstt_dataRuleTableInfo; }
            set { _jstt_dataRuleTableInfo = value; }
        }

        [XmlElement("JSTT_RuleDataItems")]
        public JSTT_DataRuleItems JSTT_RuleDataItems
        {
            get { return _jstt_dataRuleItems; }
            set { _jstt_dataRuleItems = value; }
        }

        [XmlElement("JSTT_EXPTDataItems")]
        public JSTT_EXPTDataItems JSTT_EXPTDataItems
        {
            get { return _jstt_exptDataItems; }
            set { _jstt_exptDataItems = value; }
        }

        public JSTT_DataRuleFormats()
        {
            _jstt_dataRuleDescription = new JSTT_DataRuleDescription();
            _jstt_dataRuleTableInfo = new JSTT_DataRuleTableInfo();
            _jstt_dataRuleItems =  new JSTT_DataRuleItems();
            _jstt_exptDataItems = new JSTT_EXPTDataItems();
        }

        public void SetRuleFormatDescription(string ruleName, string ruleDataSendMode, int ruleDataMinLength,
            int ruleDataMaxLength)
        {
            _jstt_dataRuleDescription.RuleName = ruleName;
            _jstt_dataRuleDescription.RuleDataSendMode = ruleDataSendMode;
            _jstt_dataRuleDescription.RuleDataMinLength = ruleDataMinLength;
            _jstt_dataRuleDescription.RuleDataMaxLength = ruleDataMaxLength;
        }

        public void SetRuleTableName(string historyTableName,string currencyTableName)
        {
            _jstt_dataRuleTableInfo.HistoryTableName = historyTableName;
            _jstt_dataRuleTableInfo.CurrencyTableName = currencyTableName;
        }

        public void AddRuleTableColumn(DataRuleTableColumn dtc)
        {
            _jstt_dataRuleTableInfo.AddTableColumn(dtc);
        }

        public void AddRuleTableColumn(string name, string displayName, string columnType,
            int typeLength, int autoIncrease = 0, string columnIsKey = "false")
        {
            _jstt_dataRuleTableInfo.AddTableColumn(new DataRuleTableColumn(name, displayName, columnType, typeLength,
                 autoIncrease, columnIsKey)); 
        }

        /// <summary>
        /// 根据DefineColumnName找到对应的DataRuleItem节点
        /// </summary>
        /// <param name="colName"></param>
        /// <returns></returns>
        public DataRuleItem GetDataRuleItemByColumName(string colName)
        {
            foreach (DataRuleItem item in JSTT_RuleDataItems.RuleItems)
            {
                if (item.ItemProperty.DefineColumnName == colName)
                    return item;
            }
            return null;
        }

      
    }
}
